/**
 * 播种面板
 */
class LandItem extends ItemRendererBase {

    public constructor() {
        super();
        this.skinName = LandItemSkin;
        this.psx = this.imgPlant.scaleX
        this.psY = this.imgPlant.scaleY
        EventManager.instance.addEvent(EventName.PLANT_GAIN_CHANGE, this.gainAnimation, this, true);
        EventManager.instance.addEvent(EventName.PLANT_PLAY_CHANGE, this.initData, this, true);
        EventManager.instance.addEvent(EventName.MANURE_STATUS_CHANGE, this.manureChange, this, true);
    }


    // 锁定土地
    private imgLandbg: eui.Image;
    // 组
    private grpMain: eui.Group;
    // 果实图片
    private imgPlant: eui.Image;
    // 进度条
    private bar: eui.ProgressBar;
    // 总数量
    private labNum: eui.Label;
    // 产出果实图片
    private imgFruit1: eui.Image; // horizontalCenter="-40" verticalCenter="30"
    private imgFruit2: eui.Image; // horizontalCenter="-40" verticalCenter="40"
    private imgFruit3: eui.Image; // horizontalCenter="40" verticalCenter="40"

    // 进度条值
    private _bar_minimum = 0;
    private _bar_maximum = 100;

    // 
    private psx: number;
    private psY: number;

    // 进度条速度
    private barSpeed: number;

    /**
     * 初始化data
     */
    private initData() {
        this.data = PlayerSelfData.landInfoSolo(this.$parent.getChildIndex(this));
        // this.dataChanged();
    }

    protected dataChanged() {
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.ontap, this);
        // 判断是否有信息
        if (typeof this.data == "undefined") {
            this.grpMain.visible = false;
            this.imgLandbg.source = "land_no_png";
            return;
        }
        // 判断是否是新开土地按钮
        if (this.data == "addLand") {
            // 新开土地按钮
            this.imgLandbg.source = "land_add_png";
            return;
        } else if (typeof this.data.plant_id == "undefined" || this.data.plant_id == 0) {
            // 已开垦空土地
            this.imgLandbg.source = "land_on_png";
            this.grpMain.visible = false;
            return;
        }
        this.grpMain.visible = true;

        // 显示作物
        this.imgPlant.source = "plant_" + this.data.plant_id + "_png";
        this.imgFruit1.source = "fruit_" + this.data.plant_id + "_png";
        this.imgFruit2.source = "fruit_" + this.data.plant_id + "_png";
        this.imgFruit3.source = "fruit_" + this.data.plant_id + "_png";

        // 进度条动画
        this.barAnimation();
    }


    /**
     * 进度条动画
     */
    private barAnimation() {

        // 处理化肥状态
        if (PlayerSelfData.manure['expire'] > Utils.time()) {
            // 有化肥状态
            if (!PlayerSelfData.manureStatus) {
                PlayerSelfData.manureStatus = true;
                EventManager.instance.dispatch(EventName.MANURE_STATUS_CHANGE, "MANURE_STATUS_CHANGE true");
                return;
            }
            // 有化肥状态
            this.bar.skinName = Bar2Skin;
            this.barSpeed = this.data.interval * 50;
        } else {
            // 没有化肥的状态
            if (PlayerSelfData.manureStatus) {
                PlayerSelfData.manureStatus = false;
                EventManager.instance.dispatch(EventName.MANURE_STATUS_CHANGE, "MANURE_STATUS_CHANGE false");
                return;
            }
            this.bar.skinName = BarSkin;
            this.barSpeed = this.data.interval * 1000;
        }

        this.labNum.text = this.data.have_num + "/" + this.data.max_num;
        egret.Tween.removeTweens(this.bar);
        egret.Tween.removeTweens(this.imgPlant);

        // 生长中
        if (this.data.have_num < this.data.max_num) {
            // // 进度条动画
            // this.bar.value = (Utils.time() - this.data.lasttime) / this.data.interval * this._bar_maximum;
            // egret.Tween.get(this.bar)
            //     .to({
            //         "value": this._bar_maximum
            //     }, (this.data.interval + this.data.lasttime - Utils.time()) * 1000)
            //     // 0.5延迟
            //     .call(() => {
            //         egret.Tween.removeTweens(this.bar);
            //         // +1动画
            //         this.addnumAnimation()

            //         PlayerSelfData.setLandInfoHave(this.data.id, (e) => {
            //             this.data = e;
            //             this.dataChanged();
            //         })
            //     })
            this.bar.value = 0;
            egret.Tween.get(this.bar).to({
                "value": this._bar_maximum
            }, this.barSpeed).call(() => {
                // egret.Tween.removeTweens(this.bar);
                // +1动画
                // this.addnumAnimation();
                // 产出果实动画
                this.addfruitAnimation();
                this.bar.value = 0;
                // 判断化肥状态
                this.manureChange();
                // 自调
                this.barAnimation();
            })

            // 植物动画
            // egret.Tween.get(this.imgPlant, { "loop": true })
            //     .to({
            //         "scaleX": this.psx * 0.8,
            //         "scaleY": this.psx * 0.8
            //     }, 1300).to({
            //         "scaleX": this.psx,
            //         "scaleY": this.psx
            //     }, 1300)
        } else {
            // 已丰收 
            // 植物动画
            // egret.Tween.get(this.imgPlant, { "loop": true })
            //     .to({
            //         "scaleX": this.psx * 1.1,
            //         "scaleY": this.psx * 1.1,
            //         "alpha": 0.7
            //     }, 800).to({
            //         "scaleX": this.psx,
            //         "scaleY": this.psx,
            //         "alpha": 1
            //     }, 800)
        }
    }


    private ontap() {
        // 判断是否是新开土地按钮
        if (typeof this.data == "undefined" || this.data == "addLand") {
            // 开垦土地
            SceneManager.ins.pushScene(new AddlandPanel);
            return;
        } else if (typeof this.data.plant_id == "undefined" || this.data.plant_id == 0) {
            // 已开垦空土地
            // 打开播种
            PlayerSelfData.tap_land_id = this.data.id;
            MainMenu.ins.plantTap();
            return;
        } else {
            // 有植物的土地 采摘
            Net.ins.send(['harvest'], { "land_id": this.data['id'] }, (e) => {
                // 播放采摘动画
                // PlayerSelfData.plant_gain = this.$parent.getChildIndex(this);
                PlayerSelfData.plant_gain = PlayerSelfData.landInfoSolo(this.$parent.getChildIndex(this))["id"];
                PlayerSelfData.setLandInfo(this.$parent.getChildIndex(this), e);
                EventManager.instance.dispatch(EventName.PLANT_GAIN_CHANGE, this.$parent.getChildIndex(this));
            });

            return;
        }
    }


    /**
     * +1动画
     */
    // private addnumAnimation() {
    //     var addnum = new eui.Label();
    //     addnum.text = "+1";
    //     addnum.size = 10;
    //     addnum.x = 100;
    //     addnum.y = 25;
    //     addnum.textColor = 0xffff00;
    //     addnum.bold = true;
    //     addnum.stroke = 1;
    //     addnum.strokeColor = 0xff0000;
    //     addnum.alpha = 0;
    //     this.grpMain.addChild(addnum);

    //     egret.Tween.get(addnum)
    //         .to({
    //             "alpha": 1,
    //             "y": 15,
    //             "size": 16,
    //         }, 300).to({
    //             "alpha": 0,
    //             "y": 5,
    //             "size": 20,
    //         }, 300).call(() => {
    //             this.grpMain.removeChild(addnum);
    //         })
    // }


    /**
     * 产出果实动画
     */
    private addfruitAnimation() {
        if (!this.imgFruit1.visible) {
            this.doaddfruitAnimation(this.imgFruit1, 40, -40, 180);
        } else if (!this.imgFruit2.visible) {
            this.doaddfruitAnimation(this.imgFruit2, -40, -40, 270);
        } else if (!this.imgFruit3.visible) {
            this.doaddfruitAnimation(this.imgFruit3, -40, 30, 90);
        }
    }


    /**
     * 产出果实动画动画
     */
    private doaddfruitAnimation(obj: eui.Image, h: number, v: number, r: number) {
        obj.visible = true;
        obj.scaleX = 0.5;
        obj.scaleY = 0.5;
        egret.Tween.get(obj).to({
            "horizontalCenter": h,
            "verticalCenter": v,
            "rotation": r
        }, 1000).call(e => {
            egret.Tween.removeTweens(obj)
            egret.Tween.get(obj, { "loop": true }).to({
                "rotation": obj.rotation + 360
            }, 2500)
        })
    }


    /**
     * 收获动画
     */
    private gainAnimation() {
        // if (typeof this.data == "undefined" || typeof this.data['id'] == "undefined" || this.data['id'] != PlayerSelfData.landInfoSolo(PlayerSelfData.plant_gain)["id"]) {
        if (typeof this.data == "undefined" || typeof this.data['id'] == "undefined" || this.data['id'] != PlayerSelfData.plant_gain) {
            this.initData();
            return;
        }
        this.initData();
        if (this.imgFruit1.visible) {
            this.dogainAnimation(this.imgFruit1)
        }
        // else {
        //     this.initData();
        // }
        if (this.imgFruit2.visible) {
            egret.setTimeout(function () {
                this.dogainAnimation(this.imgFruit2)
            }, this, 200)
        }
        if (this.imgFruit3.visible) {
            egret.setTimeout(function () {
                this.dogainAnimation(this.imgFruit3)
            }, this, 400)
        }
    }


    /**
     * 收获动画动画
     */
    private dogainAnimation(obj: eui.Image) {
        egret.Tween.removeTweens(obj)
        egret.Tween.get(obj).to({
            "horizontalCenter": 0,
            "verticalCenter": 0,
            "rotation": 0,
            "alpha": 0
        }, 300).wait(300).call(e => {
            egret.Tween.get(obj).to({
                "verticalCenter": -100,
                "alpha": 0.7,
                "scaleX": 0.7,
                "scaleY": 0.7
            }, 400).to({
                "verticalCenter": -30,
                "alpha": 1,
                "scaleX": 1,
                "scaleY": 1
            }, 300).wait(900).call(e => {
                obj.scaleX = 0.5;
                obj.scaleY = 0.5;
                obj.verticalCenter = 0;
                obj.visible = false;
                // this.initData();
            })
        })
    }


    /**
     * 化肥状态改变
     */
    private manureChange() {
        this.dataChanged();
        // if (PlayerSelfData.manure['expire'] > Utils.time()) {
        //     this.bar.skinName = Bar2Skin;
        //     this.barSpeed = this.data.interval * 50;
        // } else {
        //     this.bar.skinName = BarSkin;
        //     this.barSpeed = this.data.interval * 1000;
        // }
    }
}